package com.enjoy.xbase.db.impi;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.enjoy.xbase.db.annotation.AnnFiled;
import com.enjoy.xbase.db.handle.IUpdate;
import com.enjoy.xbase.db.tools.DBTools;
import com.enjoy.xbase.db.tools.LogDb;

import java.lang.reflect.Field;

public class MUpdate implements IUpdate {
    private SQLiteDatabase db;
    private String tableName;
    private String selection;
    private String[] selectionArgs;

    public MUpdate(SQLiteDatabase db) {
        this.db = db;
    }
    public MUpdate(SQLiteDatabase db,String tabName) {
        this.db = db;
        this.tableName=tabName;
    }

    @Override
    public IUpdate where(String selection, String... selectionArgs) {
        this.selection = selection;
        this.selectionArgs = selectionArgs;
        return this;
    }

    @Override
    public int update(ContentValues contentValues) {
        if (TextUtils.isEmpty(tableName)){
            throw new RuntimeException("tableName未设置");
        }
        return db.update(tableName, contentValues, selection, selectionArgs);
    }

    @Override
    public int update(Object data) {
        Field[] fields = data.getClass().getDeclaredFields();
        ContentValues contentValues = new ContentValues();
        int dataId=-1;
        try {
            for (Field _field : fields) {
                AnnFiled annFiled = _field.getAnnotation(AnnFiled.class);
                if (annFiled != null) {
                    _field.setAccessible(true);
                    if (annFiled.columnName().equals("id")) {//主键
                        Object val=_field.get(data);
                        if (val instanceof Integer) {
                            dataId= (int) val;
                        }else{
                            throw new RuntimeException(data.getClass().getSimpleName()+"数据对象主键不为整型");
                        }
                    }else{//非主键
                        put(contentValues, annFiled.columnName(), _field.get(data));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogDb.err("数据库异常", "插入异常" + e.toString());
            return 0;
        }
        if (TextUtils.isEmpty(selection)){
            return db.update(DBTools.getTabName(data), contentValues, "id=?", new String[]{dataId+""});
        }else{
            return db.update(DBTools.getTabName(data), contentValues, selection, selectionArgs);
        }
    }
    private static void put(ContentValues contentValues, String columnName, Object val) {
        if (val == null) return;
        if (val instanceof Integer) {
            contentValues.put(columnName, (Integer) val);
        } else if (val instanceof Long) {
            contentValues.put(columnName, (Integer) val);
        } else if (val instanceof String) {
            contentValues.put(columnName, (String) val);
        } else if (val instanceof Float) {
            contentValues.put(columnName, (Float) val);
        } else if (val instanceof Double) {
            contentValues.put(columnName, (Double) val);
        } else {
            throw new RuntimeException("数据格式未知");
        }
    }
}
